﻿using AutoMapper;
using KayakMes.Common;
using KayakMes.Common.Dtos.TeamDTO.Input;
using KayakMes.Common.Dtos.TeamDTO.Output;
using KayakMes.Common.Result;
using KayakMes.ProcessManagement.Domain.SchedulingModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yitter.IdGenerator;

namespace KayakMes.ProcessManagement.Application.Scheduling
{
    public class TeamSetService : ITeamSetService
    {
        private readonly BaseReposiotry<TeamSetModel> _teamSetRepository;
        private readonly BaseReposiotry<TeamTypeModel> _teamTypeRepository;
        private readonly IMapper _mapper;

        public TeamSetService(BaseReposiotry<TeamSetModel> teamSetRepository, IMapper mapper, BaseReposiotry<TeamTypeModel> teamTypeRepository)
        {
            _teamSetRepository = teamSetRepository;
            _mapper = mapper;
            _teamTypeRepository = teamTypeRepository;
        }
        /// <summary>
        /// 添加班组信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public ApiResult<int> Add(TeamSetInput input)
        {
            var entity = _mapper.Map<TeamSetModel>(input);
            entity.Id = YitIdHelper.NextId();
            var res = _teamSetRepository.Add(entity);
            if (res > 0)
            {
                return new ApiResult<int>()
                {
                    Code = ApiEnums.Success,
                    Message = "添加团队信息成功",
                    Data = res
                };
            }
            else
            {
                return new ApiResult<int>()
                {
                    Code = ApiEnums.Fail,
                    Message = "添加团队信息失败",
                    Data = 0
                };
            }

        }

        /// <summary>
        /// 删除班组信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ApiResult<int> DeleteTeamSet(long id)
        {
            var query = _teamSetRepository.GetModel(id);
            if (query != null)
            {
                var res = _teamSetRepository.Delete(query);
                if (res > 0)
                {
                    return new ApiResult<int>()
                    {
                        Code = ApiEnums.Success,
                        Message = "删除团队信息成功",
                        Data = res
                    };
                }
                else
                {
                    return new ApiResult<int>()
                    {
                        Code = ApiEnums.Fail,
                        Message = "删除团队信息失败",
                        Data = 0
                    };
                }
            }
            else
            {
                return new ApiResult<int>()
                {
                    Code = ApiEnums.Fail,
                    Message = "找不到要删除的团队信息！",
                    Data = -1
                };
            }
        }

        public ApiResult<TeamSetOutput> FindTeamSet(long id)
        {
            var query = _teamSetRepository.GetModel(id);
            if (query != null && id > 0)
            {
                var entity = _mapper.Map<TeamSetOutput>(query);
                return new ApiResult<TeamSetOutput>()
                {
                    Code = ApiEnums.Success,
                    Message = "查询团队信息成功",
                    Data = entity
                };
            }
            else
            {
                return new ApiResult<TeamSetOutput>()
                {
                    Code = ApiEnums.Fail,
                    Message = "找不到要查询的班组信息！",
                    Data = null
                };
            }
        }
        /// <summary>
        /// 获取班组信息列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public ApiPaging<TeamSetOutput> GetTeamSetList(GetTeamSetInput input)
        {
            var teamset = _teamSetRepository.GetAll().Where(x=>!x.IsDeleted);
            var teamtype = _teamTypeRepository.GetAll().Where(x=>!x.IsDeleted);
            if (!string.IsNullOrEmpty(input.TeamSetCode))
            {
                teamset = teamset.Where(x => x.TeamSetCode.Contains(input.TeamSetCode));
            }
            if (!string.IsNullOrEmpty(input.TeamSetName))
            {
                teamset = teamset.Where(x => x.TeamSetName.Contains(input.TeamSetName));
            }
            if (input.TeamTypeId > 0)
            {
                teamset = teamset.Where(x => x.TeamTypeId == input.TeamTypeId);
            }
            int total = teamset.Count();
            var linq = teamset.LeftJoin<TeamTypeModel>((a, b) => a.TeamTypeId == b.Id && !b.IsDeleted).Select((a, b) => new TeamSetOutput
            {
                Id = a.Id,
                TeamSetCode = a.TeamSetCode,
                TeamTypeId = a.TeamTypeId,
                TeamTypeName = b.TeamTypeName,
                TeamSetName = a.TeamSetName,
                TeamLeader = a.TeamLeader,
                Remark = a.Remark
            }).ToPageList(input.PageIndex,input.PageSize,ref total);
            return new ApiPaging<TeamSetOutput>()
            {
                Items = linq,
                Total = linq.Count(),
                Code = ApiEnums.Success,
                Message = "获取成功",
                PageIndex = input.PageIndex,
                PageSize = input.PageSize,
            };
        }
        /// <summary>
        /// 修改班组设置信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public ApiResult<int> UpdateTeamSet(UpdateTeamSetInput input)
        {
            var query = _teamSetRepository.GetModel(input.Id);
            if (query != null && input.Id > 0)
            { 
                var entity = _mapper.Map(input,query);
                var res = _teamSetRepository.Update(entity);

                if (res > 0)
                {
                    return new ApiResult<int>()
                    {
                        Code = ApiEnums.Success,
                        Message = "更新成功",
                        Data = res
                    };
                }
                else
                {
                    return new ApiResult<int>()
                    {
                        Code = ApiEnums.Fail,
                        Message = "更新失败",
                        Data = res
                    };
                }
            }
            else
            {
                return new ApiResult<int>()
                {
                    Code = ApiEnums.Fail,
                    Message = "找不到要修改的班组信息！",
                    Data = -1
                };
            }
        }
    }
}
